/*
 * @Description: 
 * @Author: 赵大壮
 * @Date: 2021-12-09 11:27:57
 * @LastEditTime: 2021-12-17 11:32:32
 * @LastEditors: 赵大壮
 */

import nyNetwork from "@/api/nyBaseNetwork";
class DZUploadFile {



    /**
     * 上传文件
     * @param base64 文件内容
     * @param progress 进度条方法
     * @param success 上传成功
     * @param error 上传失败
     */
    upload(base64: any, type: any, name: string, progress: Function, success: Function, error: Function) {
        nyNetwork.upload("上传文件", type, this.base64ToFile(base64, name),
            (index: any) => {
                progress(index);
            },
            (data: any) => {
                success(data);
            },
            () => {
                console.log("上传失败")
                error();
            })
    }

    /**
     * 上传文件
     * @param base64 文件内容
     * @param progress 进度条方法
     * @param success 上传成功
     * @param error 上传失败
     */
    uploadBigFile(base64: any, type: any, name: string, progress: Function, success: Function, error: Function) {
        nyNetwork.upload("上传文件", type, base64,
            (index: any) => {
                progress(index);
            },
            (data: any) => {
                success(data);
            },
            () => {
                console.log("上传失败")
                error();
            })
    }


    uploadImage(base64: any, progress: Function, success: Function, error: Function) {
        nyNetwork.upload("上传图片", "image", base64,
            (index: any) => {
                progress(index);
            },
            (data: any) => {
                success(data);
            },
            () => {
                console.log("上传失败")
                error();
            })
    }

    editorUploadImage(base64: any, progress: Function, success: Function, error: Function) {
        nyNetwork.upload("富文本上传图片", "image", base64,
            (index: any) => {
                progress(index);
            },
            (data: any) => {
                success(data);
            },
            () => {
                console.log("上传失败")
                error();
            })
    }

    uploadVideo(base64: any, success: Function, error: Function) {
        nyNetwork.upload("上传视频", "", base64,
            (index: any) => {
                
            },
            (data: any) => {
                success(data);
            },
            () => {
                console.log("上传失败")
                error();
            })
    }


    base64ToFile(urlData: any, name: string) {
        let arr = urlData.split(',');
        let mime = arr[0].match(/:(.*?);/)[1];
        let bytes = atob(arr[1]); // 解码base64
        let n = bytes.length
        let ia = new Uint8Array(n);
        while (n--) {
            ia[n] = bytes.charCodeAt(n);
        }
        return new File([ia], name, { type: mime });
    }

}
const uploadFile = new DZUploadFile();
export default uploadFile;